#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
标准化响应工具类
"""

from datetime import datetime

def serialize_datetime(obj):
    """递归序列化datetime对象"""
    if isinstance(obj, datetime):
        return obj.isoformat()
    elif isinstance(obj, dict):
        return {key: serialize_datetime(value) for key, value in obj.items()}
    elif isinstance(obj, list):
        return [serialize_datetime(item) for item in obj]
    else:
        return obj

def create_response(success=True, data=None, message=None, error=None, status_code=200):
    """创建标准化响应格式"""
    response = {
        'success': success,
        'timestamp': datetime.now().isoformat(),
    }
    
    if success:
        if data is not None:
            # 序列化datetime对象
            response['data'] = serialize_datetime(data)
        if message:
            response['message'] = message
    else:
        if error:
            response['error'] = error
        if message:
            response['message'] = message
    
    return response, status_code
